package com.yykj.common.vo;

import com.yykj.common.util.LocalRequestUtil;
import com.yykj.common.util.PageRequestUtil;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chendg on 2021/9/9 0009.
 */
@Data
public class PageData<T> {

    private Integer currentPage;

    private Integer pageSize;

    private Integer totalPage;

    private Long totalCount;

    private List<T> list;

    public static <T> PageData<T> formatList(Page<T> list, Integer currentPage, Integer pageSize) {
        PageData<T> ret = new PageData<>();
        ret.setList(list.getContent());
        ret.setTotalPage(list.getTotalPages());
        ret.setTotalCount(list.getTotalElements());
        ret.setCurrentPage(PageRequestUtil.getPage(currentPage) + 1);
        ret.setPageSize(PageRequestUtil.getSize(pageSize));
        return ret;
    }

    public static <T> PageData<T> defaultPageData(Integer currentPage, Integer pageSize){
        PageData<T> ret = new PageData<>();
        ret.setList(new ArrayList<>());
        ret.setTotalCount(0L);
        ret.setTotalPage(0);
        ret.setCurrentPage(currentPage);
        ret.setPageSize(pageSize);
        return ret;
    }

}
